package transactionimplementation

import (
	"fmt"
	"log"
	"reflect"
	Database "salarypayment-case/DataBase"
	"salarypayment-case/modelimplementation"
	"time"
	"strconv"
)


type SalesReceiptTransaction struct {
	itsEmpid     int
	itsSalesDate time.Time
	itsAmount float64
}

func (st *SalesReceiptTransaction) Execute() {
	e := Database.PayrollDatabases.GetEmployee(st.itsEmpid)

	if e != nil {
		pc := e.GetClassification()

		if reflect.TypeOf(pc) == reflect.TypeOf(modelimplementation.CommissionedClassification{}) {
			cc := pc.(*modelimplementation.CommissionedClassification)
			sr := modelimplementation.CreateSalesReceipt(st.itsSalesDate,st.itsAmount)
			cc.AddSalesReceipt(*sr)
		} else {
			err := fmt.Errorf(Database.PayrollExceptionMessage.NotCommissionedClassification + strconv.Itoa(e.GetEmpid()))
		    log.Println(err.Error())
		}
	} else {
		err := fmt.Errorf(Database.PayrollExceptionMessage.EmployeeNotExist + strconv.Itoa(e.GetEmpid()))
		log.Println(err.Error())
	}
}